//
//  LYSmallVideoView.m
//  LYLiveApp
//
//  Created by QD on 2018/3/1.
//  Copyright © 2018年 览益信息科技. All rights reserved.
//

#import "LYSmallVideoView.h"
#import "VHallMoviePlayer.h"
#import "OpenCONSTS.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import "LYVideoInstance.h"
#import "LYAudioView.h"

@interface LYSmallVideoView()<VHallMoviePlayerDelegate>
@property (strong,nonatomic) VHallMoviePlayer *moviePlayer;
@property(nonatomic,strong) UIActivityIndicatorView *indicator;
@property(nonatomic,strong) LYAudioView *audioView;
@property(nonatomic,strong) UILabel *noDataLabel;
@property(nonatomic,strong) UITapGestureRecognizer *tapGesture;

@end

@implementation LYSmallVideoView

+ (instancetype)viewWithXib
{
    LYSmallVideoView *view = [[[UINib nibWithNibName:@"LYSmallVideoView" bundle:nil] instantiateWithOwner:self options:nil] firstObject];
    return view;
}

- (void)setIdString:(NSString *)idString {
    
    _idString = idString;
    self.moviePlayer = [LYVideoInstance sharedInstance].moviePlayer;
   
    self.moviePlayer.movieScalingMode = kRTMPMovieScalingModeAspectFill;

    [self.containerView addSubview:self.moviePlayer.moviePlayerView];
    self.moviePlayer.moviePlayerView.frame = self.containerView.bounds;
    
    if ([LYLocalData sharedInstance].isAudio) {
        self.audioView.hidden = NO;
        [self.moviePlayer.moviePlayerView addSubview:self.audioView];
    }
    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.moviePlayer.moviePlayerView.userInteractionEnabled = YES;
    [self.moviePlayer.moviePlayerView addGestureRecognizer:self.tapGesture];
    
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
                                                    
                                                    initWithTarget:self
                                                    
                                                    action:@selector(handlePan:)];
    
    [self addGestureRecognizer:panGestureRecognizer];

    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterBackground) name:kApplicationDidEnterBackground object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterForeground) name:kApplicationDidEnterForeground object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeSmallScreen) name:kVedioLiveControllerRemoveSmallVideoScreen object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleError) name:kLYVideoInstanceHandelError object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoBeginLoading) name:kLYVideoInstanceBeginLoading object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoPlaySuccess) name:kLYVideoInstancePlaySuccess object:nil];
}

- (void) handlePan:(UIPanGestureRecognizer*) recognizer {
    
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];

    CGPoint translation = [recognizer translationInView:window.rootViewController.view];
    
    CGFloat centerX=recognizer.view.center.x+ translation.x;
    
    CGFloat thecenter=0;
    
    recognizer.view.center=CGPointMake(centerX,
                                       
                                       recognizer.view.center.y+ translation.y);
    
    [recognizer setTranslation:CGPointZero inView:window.rootViewController.view];
    
    if(recognizer.state==UIGestureRecognizerStateEnded|| recognizer.state==UIGestureRecognizerStateCancelled) {
        
        if(centerX > ScreenWidth/2) {
            
            thecenter = ScreenWidth-233/2 - 7;
            
        }else{
            
            thecenter = 233/2;
            
        }
        [UIView animateWithDuration:0.3 animations:^{
            
            recognizer.view.center=CGPointMake(thecenter,
                                               
                                               recognizer.view.center.y+ translation.y);
            
        }];
        
        [LYVideoInstance sharedInstance].smallVideoFrame = recognizer.view.frame;
    }

    
}

- (void)videoPlaySuccess {
    
    [self.noDataLabel removeFromSuperview];
    [LYUtil hideHUDTo:self.moviePlayer.moviePlayerView];
    [self.indicator stopAnimating];
    [self.indicator removeFromSuperview];
}

- (void)videoBeginLoading {
    
    [self.moviePlayer.moviePlayerView addSubview:self.indicator];
    self.indicator.center = self.moviePlayer.moviePlayerView.center;
    [self.indicator startAnimating];
}

- (void)handleError {
   
    WS(weakSelf)
    [weakSelf.moviePlayer stopPlay];
    [LYUtil hideHUDTo:weakSelf.moviePlayer.moviePlayerView];
    [weakSelf.indicator stopAnimating];
    [weakSelf.indicator removeFromSuperview];
    weakSelf.noDataLabel.hidden = NO;
    [weakSelf.moviePlayer.moviePlayerView addSubview:weakSelf.noDataLabel];
}

- (void)appEnterBackground {
    
    if (![LYVideoInstance sharedInstance].isAudio) {
        [self.moviePlayer stopPlay];
    }
}

- (void)appEnterForeground {
    
    if (![LYVideoInstance sharedInstance].isAudio) {
        NSString *idString;
        idString = [NSString stringWithFormat:@"%ld",(long)[LYLocalData sharedInstance].roomInfo.vhall.integerValue];
        NSDictionary *param = @{ @"id"               :idString,
                                 @"name"             :@"User000001",
                                 @"email"            :[LYLocalData sharedInstance].uuId
                                 };
        [self.moviePlayer startPlay:param];
    }
}

- (void)removeSmallScreen {
    
    [self deleteAction:nil];
}

- (void)tapAction:(UITapGestureRecognizer *)tapGesture {
    
    self.audioView.hidden = YES;
    [self.audioView removeFromSuperview];
    
    UITabBarController *mainVC = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    LYVedioLiveController *vc = [LYVedioLiveController new];
    vc.hidesBottomBarWhenPushed = YES;
    UINavigationController *nav = mainVC.selectedViewController;
    [nav pushViewController:vc animated:YES];
}

- (IBAction)deleteAction:(id)sender {
    
    [self removeFromSuperview];
    [self.moviePlayer stopPlay];
    [self.noDataLabel removeFromSuperview];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.moviePlayer.moviePlayerView removeGestureRecognizer:self.tapGesture];
    if (isIPhoneX) {
        self.moviePlayer.moviePlayerView.frame = CGRectMake(0, 44, SCREEN_WIDTH, (180 * SCREEN_WIDTH)/320);
    } else {
        self.moviePlayer.moviePlayerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, (180 * SCREEN_WIDTH)/320);
    }
    [_indicator stopAnimating];
    [_indicator removeFromSuperview];
    [LYUtil hideHUDTo:self.moviePlayer.moviePlayerView];
    _audioView.hidden = YES;
    [_audioView removeFromSuperview];

}

- (IBAction)toVideoControllerAction:(id)sender {
    
    self.audioView.hidden = YES;
    [self.audioView removeFromSuperview];
    [self tapAction:nil];
}

- (void)deleteView {
    
    self.audioView.hidden = YES;
    [self.audioView removeFromSuperview];
    [self deleteAction:nil];
}

#pragma mark ----- getters and setters
- (UIActivityIndicatorView *)indicator {
    
    if (nil == _indicator) {
        _indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
        _indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        _indicator.backgroundColor = [UIColor clearColor];
        _indicator.alpha = 1.0;
        _indicator.layer.cornerRadius = 6;
        _indicator.layer.masksToBounds = YES;
    }
    [_indicator setCenter:CGPointMake(self.moviePlayer.moviePlayerView.frame.size.width / 2.0, self.moviePlayer.moviePlayerView.frame.size.height / 2.0)];
    [_indicator startAnimating];
    return _indicator;
}

- (LYAudioView *)audioView {
    
    if (nil == _audioView) {
        _audioView = [LYAudioView viewWithXib];
        _audioView.frame = self.moviePlayer.moviePlayerView.bounds;
        _audioView.origin = CGPointMake(0, 0);
        _audioView.hidden = YES;
    }
    return _audioView;
}

- (UILabel *)noDataLabel {
    
    if (nil == _noDataLabel) {
       
        _noDataLabel = [[UILabel alloc] init];
        _noDataLabel.frame = CGRectMake(0, 0, 200, 20);
        _noDataLabel.textAlignment = NSTextAlignmentCenter;
        _noDataLabel.center = self.moviePlayer.moviePlayerView.center;
        _noDataLabel.textColor = [UIColor whiteColor];
        _noDataLabel.font = [UIFont fontWithName:@"" size:15.0f];
        _noDataLabel.text = @"视频加载失败~";
        _noDataLabel.hidden = YES;
    }
    return _noDataLabel;
}


@end
